package com.pbxadmin.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.pbxadmin.cfg.WSConstants;
import com.pbxadmin.pojo.DF;
import com.pbxadmin.pojo.Network;
import com.pbxadmin.service.PbxWebService;

@Controller
@RequestMapping("/system")
public class SystemController {
	
	private Logger log = LoggerFactory.getLogger(SystemController.class);
	@Autowired
	private PbxWebService pbxWebService;
	@Autowired
	private Gson gson;
	
	@RequestMapping("/network/list")
	public ModelAndView networList(){
		//
		String returnCode = pbxWebService.invokePbx("list_network");
		if(returnCode.isEmpty()){
			return new ModelAndView("/error/pbxerror","message","pbxservice 发生错误，请稍候再试。");
		}
		Network network = gson.fromJson(returnCode, Network.class);
		//
		ModelAndView mav = new ModelAndView("/system/network_list");
		mav.addObject("network",network);
		return mav;
	}
	
	@RequestMapping("/network/modify/init")
	public ModelAndView networkModInit(HttpServletRequest req){
		//
		String result = req.getParameter("result");
		if(result != null){
			result = new String(Base64.decodeBase64(result));
		}else{
			result = "";
		}
		//
		String returnCode = pbxWebService.invokePbx("list_network");
		if(returnCode.isEmpty()){
			return new ModelAndView("/error/pbxerror","message","pbxservice 发生错误，请稍候再试。");
		}
		Network network = gson.fromJson(returnCode, Network.class);
		//
		ModelAndView mav = new ModelAndView("/system/network_modify");
		mav.addObject("network", network);
		return mav;
	}
	
	@RequestMapping("/network/modify")
	public ModelAndView networkMod(HttpServletRequest request,@ModelAttribute Network network){
		//
		if(network == null){
			return new ModelAndView("/error/parameters","message","提交参数不正确");
		}
		
		String returnCode = pbxWebService.invokePbx("mod_network", new Object[]{network.getAddress(),network.getGateway(),network.getNetmask()});
		String result = "";
		if(WSConstants.RTN_SUCCESS.equals(returnCode)){
			result = "修改网卡成功";
		}else{
			result = "修改网卡失败";
		}
		result = Base64.encodeBase64URLSafeString(result.getBytes());
		return new ModelAndView(new RedirectView("/pbxadmin/system/network/modify/init"),"result",result);
	}
	
	@RequestMapping("/diskspace/list")
	public ModelAndView listDiskSpace(){
		//
		String returnCode = pbxWebService.invokePbx("sys_df");
		if(returnCode.isEmpty()){
			return new ModelAndView("/error/pbxerror","message","pbxservice 发生错误，请稍候再试。");
		}
		List<DF> list = gson.fromJson(returnCode, new TypeToken<List<DF>>(){}.getType());
		//
		ModelAndView mav = new ModelAndView("/system/diskspace");
		mav.addObject("list", list);
		return mav;
	}

	public PbxWebService getPbxWebService() {
		return pbxWebService;
	}

	public void setPbxWebService(PbxWebService pbxWebService) {
		this.pbxWebService = pbxWebService;
	}

	public Gson getGson() {
		return gson;
	}

	public void setGson(Gson gson) {
		this.gson = gson;
	}
}
